package mobile.android.portal.ui;

import java.net.ConnectException;

import mobile.android.portal.R;
import mobile.android.portal.util.Functions;
import mobile.android.portal.webservice.BusinessPortalWebService;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class CreateAccountActivity extends Activity {

	EditText name, org, address, email, telephone, username, password,
			re_password;
	Handler addNew;

	/* declaring progress dialog */
	ProgressDialog progressDialog;

	/*
	 * Declaring alert dialog to indicate if it is not possible to connect with
	 * the web service
	 */
	AlertDialog connectionFailedAlert;
	/* Declaring alert dialog to indicate if user has entered invalid input data */
	AlertDialog userInputValidateAlert;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_account);

		name = (EditText) findViewById(R.id.name);
		org = (EditText) findViewById(R.id.org);
		address = (EditText) findViewById(R.id.address);
		email = (EditText) findViewById(R.id.email);
		telephone = (EditText) findViewById(R.id.tp);
		username = (EditText) findViewById(R.id.username);
		password = (EditText) findViewById(R.id.password);
		re_password = (EditText) findViewById(R.id.re_password);
		//progressDialog = Functions.getProgressDialog(this, "Please wait...");

	}

	public void onCreateAccountButtonClick(View v) {
		Intent mainScreen = new Intent(this, MobileBusinessPortalActivity.class);
		if(name.getText().toString().length()!= 0 && org.getText().toString().length()!= 0&&address.getText().toString().length()!= 0&&telephone.getText().toString().length()!= 0&&username.getText().toString().length()!= 0&&password.getText().toString().length()!= 0&&re_password.getText().toString().length()!= 0){
			if(password.getText().toString().equals(re_password.getText().toString())){				
				
				//calling user adding task
				new WSAddNewUser().execute(null,null,null);			
				
				startActivity(mainScreen);
				//Log.v("update result", name.getText().toString()+"-"+org.getText().toString()+"-"+address.getText().toString()+"-"+telephone.getText().toString()+"-"+username.getText().toString()+"-"+password.getText().toString()+"-"+re_password.getText().toString());		
				Toast.makeText(getApplicationContext(),	"User Added Successfully...", Toast.LENGTH_SHORT).show();
				// User Added Successfully...
			}else{
				// Password and Re-password do not match..!
				Toast.makeText(getApplicationContext(),	"Password and Re-password do not match..!", Toast.LENGTH_SHORT).show();
			}
		}else{
			// All fields are required.. please fill ! 
			Toast.makeText(getApplicationContext(),	"All fields are required.. please fill ! ", Toast.LENGTH_SHORT).show();
		}
		
		//startActivity(mainScreen);
		// User Added Successfully...
	}

	public void onCancelButtonClick(View v) {
		Intent mainScreen = new Intent(this, MobileBusinessPortalActivity.class);
		startActivity(mainScreen);
	}
	
	
	class WSAddNewUser extends AsyncTask<Object, Object, Object> {

		@Override
		protected Object doInBackground(Object... params) {
			try {
				
				BusinessPortalWebService.createAccount(name.getText().toString(), org.getText().toString(), address.getText().toString(), email.getText().toString(), telephone.getText().toString(), username.getText().toString(), password.getText().toString());
				return null;

			} catch (ConnectException e) {
				/*
				 * Sends an empty message to the web service client if an
				 * ConnectException occurred
				 */
				addNew.sendEmptyMessage(-1);
				progressDialog.dismiss();
				Log.i("ConnectException", "ConnectException occured");
				e.printStackTrace();

			} catch (Exception e) {
				// TODO Auto-generated catch block
				// Send an empty message if an exception occurred
				// addProductHandler.sendEmptyMessage(-1);
				progressDialog.dismiss();
				Log.i("Exception", "Exception occured");
				e.printStackTrace();
			}
			return null;
		}
	}

}
